import { Module } from 'vuex'
import { IRoterType } from '@/store/type'
import { ISystemType } from '@/store/main/system/type'
import {
  getUserListRequest,
  deleteUserListRequest,
  createUserRequest,
  editUserRequest
} from '@/service/mian/system/system'

export const system: Module<ISystemType, IRoterType> = {
  namespaced: true,
  state: {
    userList: [],
    userTotal: 0,
    roleList: [],
    roleTotal: 0,
    goodList: [],
    goodTotal: 0,
    menuList: [],
    menuTotal: 0,
    departList: [],
    departTotal: 0
  },
  getters: {
    getListState(state) {
      return function (name: string) {
        switch (name) {
          case 'user':
            return state.userList
            break
          case 'role':
            return state.roleList
            break
          case 'good':
            return state.goodList
            break
          case 'menu':
            return state.menuList
            break
          case 'depart':
            return state.departList
            break
          default:
            break
        }
      }
    },
    getTotalState(state) {
      return function (name: string) {
        switch (name) {
          case 'user':
            return state.userTotal
            break
          case 'role':
            return state.roleTotal
            break
          case 'good':
            return state.goodTotal
            break
          case 'menu':
            return state.menuTotal
            break
          case 'depart':
            return state.departTotal
          default:
            break
        }
      }
    }
  },
  mutations: {
    changeUserList(state, list) {
      state.userList = list
    },
    changeUserTotal(state, total) {
      state.userTotal = total
    },
    changeRoleList(state, list) {
      state.roleList = list
    },
    changeRoleTotal(state, total) {
      state.roleTotal = total
    },
    changeGoodList(state, list) {
      state.goodList = list
    },
    changeGoodTotal(state, total) {
      state.goodTotal = total
    },
    changeMenuList(state, list) {
      state.menuList = list
    },
    changeMenuTotal(state, total) {
      state.menuTotal = total
    },
    changeDepartList(state, list) {
      state.departList = list
    },
    changeDepartTotal(state, total) {
      state.departTotal = total
    }
  },
  actions: {
    async getSystemData({ commit }, info: any) {
      let currenturl = ''
      switch (info.pagesName) {
        case 'user':
          currenturl = '/users/list'
          break
        case 'role':
          currenturl = '/role/list'
          break
        case 'good':
          currenturl = '/goods/list'
          break
        case 'menu':
          currenturl = '/menu/list'
          break
        case 'depart':
          currenturl = '/department/list'
          break
        default:
          break
      }
      const res = await getUserListRequest(currenturl, info.configInfo)
      const { list, totalCount } = res.data
      switch (info.pagesName) {
        case 'user':
          commit('changeUserList', list)
          commit('changeUserTotal', totalCount)
          break
        case 'role':
          commit('changeRoleList', list)
          commit('changeRoleTotal', totalCount)
          break
        case 'good':
          commit('changeGoodList', list)
          commit('changeGoodTotal', totalCount)
          break
        case 'menu':
          commit('changeMenuList', list)
          commit('changeMenuTotal', totalCount)
          break
        case 'depart':
          commit('changeDepartList', list)
          commit('changeDepartTotal', totalCount)
          break
        default:
          break
      }
    },
    async deletePageData({ dispatch }, info: any) {
      let currenturl = ''
      switch (info.pagesName) {
        case 'user':
          currenturl = `/users/${info.id}`
          break
        case 'role':
          currenturl = `/role/${info.id}`
          break
        case 'good':
          currenturl = `/goods/${info.id}`
          break
        case 'menu':
          currenturl = `/menu/${info.id}`
          break
        case 'depart':
          currenturl = `/department/${info.id}`
          break
        default:
          break
      }
      const res = await deleteUserListRequest(currenturl)
    },
    async createPageData({ dispatch }, info: any) {
      const currenturl = `/${info.pagesName}`
      const res = await createUserRequest(currenturl, info.configInfo)
      if (info.pagesName === 'users') info.pagesName = 'user'
      //请求最新数据
      dispatch('getSystemData', {
        pagesName: info.pagesName,
        configInfo: {
          offset: 0,
          size: 10
        }
      })
    },
    async editPageData({ dispatch }, info: any) {
      const currenturl = `/${info.pagesName}/${info.id}`
      const res = await editUserRequest(currenturl, info.configInfo)
      if (info.pagesName === 'users') info.pagesName = 'user'
      //请求最新数据
      dispatch('getSystemData', {
        pagesName: info.pagesName,
        configInfo: {
          offset: 0,
          size: 10
        }
      })
    }
  }
}
